package com.travelsky.flash.util.log;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.log4j.Logger;

import com.travelsky.flash.util.msg.SendMsgUtil;


/**
 * @Description 自定义日志记录
 * 				更方便扩展消息推送
 * @author syuan
 * @date 2015年6月4日  上午10:23:18
 * Company: TravelSky
 */
public class FlashLogger {
	
	private Logger logger = null;
	
	@SuppressWarnings("rawtypes")
	public FlashLogger(Class clazz) {
		logger = Logger.getLogger(clazz);
	}
	
	public void info(Object msg) {
		logger.info(msg);
		SendMsgUtil.sendMsg(msg.toString());
	}
	
	public void debug(Object msg) {
		logger.debug(msg);
	}
	
	public void error(Object msg) {
		logger.error(msg);
		SendMsgUtil.sendMsg(msg.toString());
	}
	
	public void error(Object msg, Throwable t) {
		logger.error(msg, t);
		SendMsgUtil.sendMsg(msg.toString());
		SendMsgUtil.sendMsg(getStackTrace(t));
	}
	public void error(Throwable t) {
		logger.error("", t);
		SendMsgUtil.sendMsg(getStackTrace(t));
	}
	
	public void warn(Object msg) {
		logger.warn(msg);
	}

	private static String getStackTrace(Throwable t) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);

		try {
			t.printStackTrace(pw);
			return sw.toString();
		} finally {
			pw.close();
			
		}
	}

}
